package com.helloUIBinder.service;

import com.helloUIBinder.model.SiteUser;
import com.helloUIBinder.repository.SiteUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service
public class SiteUserManager {

    @Autowired
    SiteUserRepository siteUserRepository;

    @Transactional(propagation = Propagation.SUPPORTS)
    public SiteUser getCurrentLogInUser() {
        List<SiteUser> loggedInUsers = siteUserRepository.findByLogged(true);
        return loggedInUsers.get(0);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public SiteUser getUserByLogin(String login) {
        return siteUserRepository.findByLogin(login);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SiteUser> getAllSiteUsers() {
        return siteUserRepository.findAll();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public SiteUser getSiteUserById(Long id) {
        return siteUserRepository.findOne(id);
    }

//    @Transactional(propagation = Propagation.SUPPORTS)
//    public SiteUser getUserByProvidedService(ProvidedService providedService) {
//        SiteUser user = siteUserRepository.findSiteUserByProvidedServiceId(providedService.getId());
//        return user;
//    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public SiteUser saveUser(SiteUser user) {
        if (user.getLogin() != null && user.getPassword() != null) {
            if (user.getName() != null) {
                if (siteUserRepository.findByLogin(user.getLogin()) == null) {
                return siteUserRepository.save(user);
                } else {
                    System.out.println("please try other login, this one is locked");
                    return null;
                }
            } else {
                System.out.println("please write your name");
                return null;
            }
        } else {
            System.out.println("please write login and password");
            return null;
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logInUser(String login, String password) {
        if (login != null && password != null) {
            SiteUser siteUser = siteUserRepository.findByLogin(login);
            if (siteUser == null) {
                System.out.println("no such login or password");
            } else {
                if (!password.equals(siteUser.getPassword())) {
                    System.out.println("no such login or password");
                } else {
                    siteUser.setLogged(true);
                    siteUserRepository.save(siteUser);
                    System.out.println("login successful");
                }
            }
        } else {
            System.out.println("please write login and password");
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logOutCurrentUser() {
        List<SiteUser> users = siteUserRepository.findByLogged(true);
        SiteUser siteUser = users.get(0);
        if (siteUser != null) {
            siteUser.setLogged(false);
            siteUserRepository.save(siteUser);
            System.out.println("logged out successful");
        } else {
            System.out.println("siteUser is null");
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<SiteUser> getSiteUsersFrom(Integer from) {
        Pageable ten = new PageRequest(0, 10);
        return siteUserRepository.findAll(ten).getContent();
    }

//    @Transactional(propagation = Propagation.SUPPORTS)
//    public List<SiteUser> findFirst() {
//        return siteUserRepository.findFirst();
//    }
//
//    @Transactional(propagation = Propagation.SUPPORTS)
//    public List<SiteUser> findSecond() {
//        return siteUserRepository.findSecond();
//    }

}
